package com.library.bookmanagement.controller;

import com.library.bookmanagement.entity.Book;
import com.library.bookmanagement.entity.User;
import com.library.bookmanagement.service.AdminService;
import com.library.bookmanagement.util.Constant;
import com.library.bookmanagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/8 16:52
 * @description：所有测试成功
 */
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@ResponseBody
@RequestMapping("/admin")
@Api(value = "管理员Controller",tags = {"管理员操作接口"})
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Value("${prop.upload-folder}")
    private String UPLOAD_FOLDER;


    /**
    * @Description: 用户修改信息返回
    * @Param: [userId]
    * @return: com.library.bookmanagement.util.ResultUtil<com.library.bookmanagement.entity.User>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "用户修改信息返回")
    @GetMapping("/userInfo")
    public ResultUtil<User> userInfo(
            @ApiParam(value = "用户修改",name = "userId")
            @RequestParam(name = "userId") Integer userId
    ){
        return adminService.queryUserById(userId);
    }

    /**
    * @Description: 用户修改信息保存
    * @Param: [user]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "用户修改信息保存")
    @PostMapping("/editUser")
    public ResultUtil editUser(
            @ApiParam(name = "user",value = "修改后的用户信息")
            @RequestBody User user
    ){
        return adminService.editUser(user);
    }
    
    /**
    * @Description: 用户添加，单个添加，用户添加不包含批量添加
    * @Param: [user]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "用户添加，单个添加，用户添加不包含批量添加")
    @PostMapping("/addUser")
    public ResultUtil addUser(
            /*@ApiParam(value = "添加用户Json对象",name = "user")
            @RequestBody User user*/
            @RequestParam("uName") String uName,
            @RequestParam("uEmail") String uEmail
    ){
        User user = new User();
        user.setuName(uName);
        user.setuEmail(uEmail);
        user.setuPassword(Constant.EDIT_ADMIN_PASSWORD);
       return adminService.addUser(user);
   }
   
   /**
   * @Description: 用户结果集返回
   * @Param: [pageNum]
   * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
   * @Author: ZXM
   * @Date: 2020/6/10
   */
    @ApiOperation(value = "用户结果集返回")
    @GetMapping("/queryCanUseUser")
    public ResultUtil<Map<String,Object>> queryCanUseUser(
            @ApiParam(value = "第几页",name = "pageNum")
            @RequestParam(name = "pageNum") Integer pageNum){
        return adminService.queryCanUse(pageNum, Constant.pageSize);
    }

    /**
    * @Description: 用户注销
    * @Param: [userId]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/12
    */
    @ApiOperation(value = "用户注销以及恢复用户身份")
    @GetMapping("/logOffUser")
    public ResultUtil logOffUser(
            @ApiParam(value = "用户Id",name = "userId")
            @RequestParam(name = "userId") Integer userId,
            @ApiParam(value = "用户状态识别[0->正常，1->注销]",name = "uBoolean")
            @RequestParam(name = "uBoolean") Integer uBoolean
    ){
        return adminService.logOffUser(userId,uBoolean);
    }

    
    /**
    * @Description: 用户密码重置
    * @Param: [userId, newPassword]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "用户密码重置")
    @PostMapping("/editPassword")
    public ResultUtil editPassword(
            @ApiParam(value = "用户ID",name = "userId")
            @RequestParam(name = "userId") Integer userId,
            @ApiParam(value = "新密码",name ="newPassword")
            @RequestParam(name = "newPassword") String newPassword
    ){
        User user = new User();
        user.setuId(userId);
        user.setuPassword(newPassword);
        return adminService.editUserPaswword(user);
    }

   /**
   * @Description: 删除用户
   * @Param: [ids]
   * @return: com.library.bookmanagement.util.ResultUtil
   * @Author: ZXM
   * @Date: 2020/6/10
   */
   @ApiOperation(value = "删除用户")
    @GetMapping("/deleteUser")
    public ResultUtil deleteUser(
            @ApiParam(value = "用户id值为数组",name = "ids")
            @RequestParam(name = "ids") List<Integer> ids
   ){
        return adminService.deleteUserById(ids);
   }

   /**
   * @Description: 图书添加,单独添加
   * @Param: [book]
   * @return: com.library.bookmanagement.util.ResultUtil
   * @Author: ZXM
   * @Date: 2020/6/10
   */
   @ApiOperation(value = "图书添加,单独添加")
    @PostMapping("/addBook")
    public ResultUtil addBook(
           @ApiParam(value = "bookJson对象",name = "book")
           @RequestBody Book book
           ){
        return adminService.addBook(book);
   }
   
    /**
    * @Description: 图书删除，可以多个同时删除
    * @Param: [ids]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
   @ApiOperation(value = "图书删除，可以多个同时删除")
    @GetMapping("/deleteBook")
    public ResultUtil deleteBook(
            @ApiParam(value = "图书编号",name = "ids")
            @RequestParam(name = "ids") List<Integer> ids
   ){
        return adminService.deleteBookById(ids);
   }

   /**
   * @Description: 回显已选择的图书可修改的基本信息
   * @Param: [bookId]
   * @return: com.library.bookmanagement.util.ResultUtil<com.library.bookmanagement.entity.Book>
   * @Author: ZXM
   * @Date: 2020/6/10
   */
    @ApiOperation(value = "回显已选择的图书可修改的基本信息")
    @GetMapping("/bookInfo")
    public ResultUtil<Book> bookInfo(
            @ApiParam(value = "已选择图书编号",name = "bookId")
            @RequestParam(name = "bookId") Integer bookId
    ){
        return adminService.queryBookById(bookId);
    }

    /**
    * @Description: 将修改信息保存到数据库
    * @Param: [book]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "将修改信息保存到数据库")
    @PostMapping("/editBookInfo")
    public ResultUtil editBookInfo(
            @ApiParam(value = "修改后的bookJson对象",name = "book")
            @RequestBody Book book
    ){
        return adminService.editBook(book);
    }

   /**
   * @Description: 管理员显示图书列表
   * @Param: [pageNum]
   * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
   * @Author: ZXM
   * @Date: 2020/6/10
   */
   @ApiOperation(value = "管理员显示图书列表")
    @GetMapping("/queryBookHasLoad")
    public ResultUtil<Map<String, Object>> queryBookHasLoad(
           @ApiParam(value = "第几页",name = "pageNum")
           @RequestParam(name = "pageNum") Integer pageNum
   ){
       return adminService.queryBookIsLoad(pageNum,Constant.pageSize);
   }

   /**
   * @Description: 图书上下架
   * @Param: [bookId]
   * @return: com.library.bookmanagement.util.ResultUtil
   * @Author: ZXM
   * @Date: 2020/6/12
   */
    @ApiOperation(value ="图书上下架")
    @GetMapping("/pullOffBook")
    public ResultUtil PullOffBook(
            @ApiParam(value = "图书ID",name = "bookId")
            @RequestParam(name = "bookId") Integer bookId,
            @ApiParam(value = "上下架参数[0->上架，1->下架]",name = "bBoolean")
            @RequestParam(name = "bBoolean") Integer bBoolean
    ){
        return adminService.pullOffBookById(bookId,bBoolean);
    }

}
